package com.qf.service;

import com.qf.config.IdWorker;
import com.qf.constant.RabbitMQConstants;
import com.qf.constant.ReportStateConstants;
import com.qf.constant.SourceType;
import com.qf.form.SmsInterfaceForm;
import com.qf.model.StandardSubmit;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author lixu
 */
@Service
public class IntefaceServiceImpl implements IntefaceService {

    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private IdWorker idWorker;
    /**
     * 封装对象  发送消息到MQ中
     * @param smsInterfaceForm
     */
    @Override
    public void submitInterfaceForm(SmsInterfaceForm smsInterfaceForm) {
        //发消息
        System.out.println("======发消息");
        //手机号
        String mobile = smsInterfaceForm.getMobile();
        if(!StringUtils.isEmpty(mobile)){
            String[] phones = mobile.split(",");
            for (String phone : phones){
                StandardSubmit  standardSubmit = new StandardSubmit();
                //当前Submit的唯一标识
                standardSubmit.setSequenceId(String.valueOf(idWorker.nextId()));// "" + null == "null"
                standardSubmit.setClientID(Integer.parseInt(smsInterfaceForm.getClientID()));
                standardSubmit.setSrcSequenceId(Long.parseLong(smsInterfaceForm.getSrcID()));
                //手机号
                standardSubmit.setDestMobile(phone);
                standardSubmit.setMessageContent(smsInterfaceForm.getContent());
                standardSubmit.setReportState(ReportStateConstants.WAIT);

                standardSubmit.setSendTime(new Date());
                //发送方式
                standardSubmit.setSource(SourceType.HTTP);
                //发一条消息  : 五大模式  主题    交换机(pre_send_sms_topic_exchange)  ---#--》  队列(pre_send_sms_topic)
                rabbitTemplate.convertAndSend(
                        RabbitMQConstants.TOPIC_PRE_SEND + "_exchange",//交换机
                        "",//RoutingKey    #
                        standardSubmit);
            }
        }
    }
}
